import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;

public class GameViewDialog extends JDialog {
	
	Game game;
	JButton[][] field;
	
	public GameViewDialog(Game g) {
		super((JDialog)null, "Game#" + g.hashCode() + ": " + g.getInfo());
		game = g;
		char[][] charField = g.getField();
		int x = charField.length;
		int y = charField[0].length;
		setLayout(new GridLayout(x, y));
		
		field = new JButton[x][y];
		for (int i = 0; i < x; ++i) {
			for (int j = 0; j < y; ++j) {
				field[i][j] = new JButton();
				field[i][j].setEnabled(false);
				if (charField[i][j] != 0)
					field[i][j].setText(charField[i][j]+"");
				add(field[i][j]);
			}
		}

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				dispose();
				game.removeDlg();
				super.windowClosing(e);
			}
		});
		
		setSize(300, 300);
		setVisible(true);
	}
	
	public void showMove(int x, int y) {
		field[x][y].setText(game.getField()[x][y]+"");
	}

}
